package com.aind.book.controller;

import com.aind.book.model.Book;
import com.aind.book.service.BookService;
import com.aind.common.context.ContextUtil;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lpeng on 2017/2/24.
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 根据类别id查询此类书籍，并跳转到列表页面
     * @param categoryId
     * @return
     */
    @RequestMapping("/toBookList")
    public ModelAndView toBookList(@RequestParam(value = "categoryId", required = true) Long categoryId){
        ModelAndView mav = new ModelAndView("/jsps/book/list");
        mav.addObject("categoryId", categoryId);
        return mav;
    }

    @ResponseBody
    @RequestMapping(value = "/getBookListByCategoryIdPager")
    public Map<String, Object>  getBookListByCategoryIdPager(@RequestParam(value = "categoryId", required = true) Long categoryId){
        Map<String, Object> jsonMap = new HashMap<>();
        List<Book> bookListByCategoryId = bookService.getBookListByCategoryId(categoryId);
        List<Map<String, Object>> list = new ArrayList<>();
        for (Book b : bookListByCategoryId) {
            Map<String, Object> m = new HashMap<String, Object>();
            m.put("bookId", b.getBookId());
            m.put("imageB", b.getImageB());
            m.put("currentPrice", b.getCurrentPrice());
            m.put("price", b.getPrice());
            m.put("discount", b.getDiscount());
            m.put("discount", b.getDiscount());
            m.put("bookName", b.getBookName());
            m.put("author", b.getAuthor());
            m.put("press", b.getPress());
            m.put("publishTime", DateFormatUtils.format(b.getPublishTime(), "yyyy-MM-dd"));
            list.add(m);
        }
        jsonMap.put("total", ContextUtil.getPagerTotal());
        jsonMap.put("list", list);
        return jsonMap;
    }

    /**
     * 根据bookId加载书籍
     * @param bookId
     * @return
     */

    @RequestMapping("/loadBookById/${account}/${status}")
    public ModelAndView loadBookById(@PathVariable String account,@PathVariable String status, @RequestParam(value = "bookId", required = true) Long bookId){
        System.out.println(account);
        System.out.println(status);
        ModelAndView mav = new ModelAndView("/jsps/book/desc");
        if (null != bookId){
            Book book = bookService.getBookByBookId(bookId);
            mav.addObject("book", book);
        }
        return mav;
    }
}
